package com.onpositive.page.pagerendering;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

public class EditPageContentRenderer {

	public final static String FIELD_WITH_CONTENT_NAME = "commitData";

	private String resContent;
	private String handlerURI;
	private String path;
	public EditPageContentRenderer(String resContent, String handlerURI,
			String currPath) {
		this.resContent = resContent;
		this.handlerURI = handlerURI;
		this.path = currPath;
	}

	public String getContent() {
		StringBuilder sb = new StringBuilder();
		sb.append("<form id=\"editForm\" action=\"" + handlerURI
				+ "\" method=\"post\">");
		sb.append("<table>");
		sb.append("<tr>");
		sb.append("<td>");
		sb.append("<textarea id=\"" + FIELD_WITH_CONTENT_NAME + "\" name=\""
				+ FIELD_WITH_CONTENT_NAME + "\"" + ">" + resContent
				+ "</textarea>");
		sb.append("</td>");
		sb.append("</tr>");
		sb.append("<tr>");
		sb.append("<td>");
		sb.append("<input type=\"submit\" value=\"Commit\">");// style=\"position:
		// absolute;
		// top: 400px;\"
		sb.append("</td>");
		sb.append("</tr>");
		sb.append("</table>");
		sb.append(CodeMirrorParserRenderer.getParserContent(path,
				path.length() == 0 ? CodeMirrorParserRenderer.XML_TYPE
						: CodeMirrorParserRenderer.CSS_TYPE,
				FIELD_WITH_CONTENT_NAME));
		sb.append("</form>");

		return sb.toString();
	}

	public InputStream getContentAsStream() {
		return new ByteArrayInputStream(getContent().getBytes());
	}
}
